package cn.chengpengper.transfer.listener;

import cn.chengpengper.transfer.storage.TranslatorCache;
import cn.chengpengper.transfer.window.TranslatorToolsWindow;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.ex.ToolWindowManagerListener;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class InitTranslatorWindowListener implements ToolWindowManagerListener {

    private final Project project;

    private static boolean init = false;

    public InitTranslatorWindowListener(Project project) {
        this.project = project;
    }

    @Override
    public void toolWindowsRegistered(@NotNull List<String> ids, @NotNull ToolWindowManager toolWindowManager) {
        ToolWindowManagerListener.super.toolWindowsRegistered(ids, toolWindowManager);
        init = true;
    }

    @Override
    public void toolWindowShown(@NotNull ToolWindow toolWindow) {
        if (!toolWindow.getId().equals("Translator")) {
            return;
        }
        if (init) {
            TranslatorCache.getInstance(project).transCache.forEach(TranslatorToolsWindow::addNote);
        }
        init = false;
    }
}
